package com.czwx.bee.base.web.validator;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Date;

/**
 * @Author:wiki
 * @Date:Create in 2018/7/10 0010 下午 3:22
 */
public class DateCompareVali  implements ConstraintValidator<DateCompare,Date> {

    String Date;

    String type;

    @Override
    public void initialize(DateCompare constraintAnnotation) {
          this.Date=constraintAnnotation.Date();
          this.type=constraintAnnotation.type();
    }

    @Override
    public boolean isValid(Date o, ConstraintValidatorContext constraintValidatorContext) {
          Boolean flag=false;
          if(StrUtil.isEmpty(Date)||StrUtil.isEmptyIfStr(o)){
              return true;
          }
          Date date=DateUtil.parseDate(Date);
          if(type.equals("min")){
              if(date.getTime()<=o.getTime()){
                   flag=true;
              }
          }else if(type.equals("max")){
              if(date.getTime()>=o.getTime()){
                  flag=true;
              }
          }

        return flag;
    }
}
